﻿#region Imports
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using CommonUtilities.Extensions;
#endregion

namespace CommonUtilities.Controls
{
    public class CUtil_WatermarkTextBox : TextBox
    {
        #region Declarations
        Brush orgForeground;
        TextAlignment align;
        #endregion Declarations

        #region Properties
        /// <summary>
        /// Text to be displayed as watermark
        /// </summary>
        public string WatermarkText { get; set; }

        /// <summary>
        /// Alignment of watermark
        /// </summary>
        public TextAlignment WatermarkAlignment { get; set; }

        /// <summary>
        /// Foreground colour of watermark
        /// </summary>
        public Brush WatermarkForeground { get; set; }

        /// <summary>
        /// Specifies if text is entered
        /// </summary>
        public bool HasText { get; private set; }

        /// <summary>
        /// Specifies text that has been entered
        /// </summary>
        public string ValueText { get; private set; }
        #endregion Properties

        #region Constructors
        public CUtil_WatermarkTextBox()
            : base()
        {
            orgForeground = this.Foreground;
            align = this.TextAlignment;
            WatermarkAlignment = TextAlignment.Center;
            WatermarkForeground = Brushes.Gray;

            FocusChanged(false);

            this.GotFocus += new RoutedEventHandler(CUtil_WatermarkTextBox_GotFocus);
            this.LostFocus += new RoutedEventHandler(CUtil_WatermarkTextBox_LostFocus);
            this.TextChanged += new TextChangedEventHandler(CUtil_WatermarkTextBox_TextChanged);
            this.Loaded += new RoutedEventHandler((s, e) => { FocusChanged(false); });
        }
        #endregion Constructors

        #region Formatting events
        private void CUtil_WatermarkTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            FocusChanged(true);
        }

        private void CUtil_WatermarkTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            FocusChanged(false);
        }

        private void CUtil_WatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            FocusChanged(true);
            HasText = (this.Text != WatermarkText);
            ValueText = (HasText ? this.Text : String.Empty);
        }
        #endregion Formatting events

        #region Methods
        private void FocusChanged(bool hasFocus)
        {
            if (supress) return;

            if (this.Text.IsNullOrEmpty() | this.Text == WatermarkText)
            {
                if (hasFocus)
                {
                    supress = true; this.Clear(); supress = false;
                    this.Foreground = orgForeground; this.TextAlignment = align;
                }
                else
                {
                    supress = true; this.Text = WatermarkText; supress = false;
                    this.Foreground = WatermarkForeground; this.TextAlignment = WatermarkAlignment;
                }
            }
            //else if (this.Text != WatermarkText) { orgForeground = this.Foreground; align = this.TextAlignment; }
            else if (this.Text != WatermarkText) { this.Foreground = orgForeground; this.TextAlignment = align; }
        }
        #endregion Methods

        bool supress = false;
    }
}
